#include "VideoImage.h"

VideoImage::VideoImage()
{
    m_VideoBtn       = new QPushButton();
    m_ImageBtn       = new QPushButton();
    m_ChooseFilePath = new QPushButton();
    m_OpenFilePath   = new QPushButton();
    m_VideoImagePath = new QLineEdit();
    m_path           = new QLabel();
    m_OneBtnLayout   = new QHBoxLayout();
    m_TwoFileLayout  = new QHBoxLayout();

    m_OneBtnLayout   ->addWidget(m_VideoBtn);
    m_OneBtnLayout   ->addWidget(m_ImageBtn);
    m_TwoFileLayout  ->addWidget(m_path);
    m_TwoFileLayout  ->addWidget(m_VideoImagePath);
    m_TwoFileLayout  ->addWidget(m_ChooseFilePath);
    m_TwoFileLayout  ->addWidget(m_OpenFilePath);

    m_VideoImageLayout = new QVBoxLayout(this);
    m_VideoImageLayout->addLayout(m_OneBtnLayout);
    m_VideoImageLayout->addLayout(m_TwoFileLayout);
    this->setFixedHeight(m_FixedHeight);
    LanguageInit();

    // 从xml文件中获取到上次用户设置的保存路径
    QDomDocument document;
    QString CongfigXmlPath = QCoreApplication::applicationDirPath() + XMLPATH;
    if(XMLFunc::ReadXmlData(CongfigXmlPath, &document))
    {
        m_Path = XMLFunc::ReadElementValue(&document, XMLVideoImagePath);
    }
    // 对xml中设置的文件路径进行处理
    QFileInfo file(m_Path);
    if(!file.exists()){
        // 当前从xml文件中获取到路径异常，采取默认路径(能存储到m_Path说明路径就没有异常)
        m_Path = QCoreApplication::applicationDirPath() + XMLVideoImagePath;
    }
    m_VideoImagePath->setText(m_Path);

    connect(m_OpenFilePath  , &QPushButton::clicked, this, &VideoImage::clickedOpenFileBtn);
    connect(m_ChooseFilePath, &QPushButton::clicked, this, &VideoImage::clickedChooseFileBtn);
    connect(m_VideoBtn      , &QPushButton::clicked, this, &VideoImage::clickedVideoBtn);
    connect(m_ImageBtn      , &QPushButton::clicked, this, &VideoImage::clickedImageBrn);
}

int VideoImage::RFixedHeight()
{
    return m_FixedHeight;
}

int VideoImage::fixedWidth(ParameterKind kind, int widget)
{
    return 0;
}

bool VideoImage::setPaletteBF(const QPalette &palette)
{
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    foreach(QObject *child, this->children())
    {
        if(QWidget* childWidget = qobject_cast<QWidget*>(child))
        {
            childWidget->setPalette(palette);
            this->setAutoFillBackground(true);
        }
    }
    return true;
}

void VideoImage::LanguageInit()
{
    m_path          ->setText   (tr("路径:"));
    m_VideoBtn      ->setToolTip(tr("录制视频"));
    m_ImageBtn      ->setToolTip(tr("截取图片"));
    m_ChooseFilePath->setToolTip(tr("选择文件"));
    m_OpenFilePath  ->setToolTip(tr("打开路径"));
    m_VideoBtn      ->setText   (tr("录制视频"));
    m_ImageBtn      ->setText   (tr("截取图片"));
    m_ChooseFilePath->setText   (tr("文件"));
    m_OpenFilePath  ->setText   (tr("路径"));
}

void VideoImage::clickedOpenFileBtn()
{
    // 要打开的目录路径
    if(m_Path.isEmpty())
    {
        QMessageBox::warning(nullptr, "警告", "您将要访问空地址!!!");
        return ;
    }

    // 打开文件管理器
    bool isTrue = QDesktopServices::openUrl(QUrl::fromLocalFile(m_Path));
    if(!isTrue){
        QMessageBox::warning(nullptr, "警告", "您访问的地址错误!!!");
        return ;
    }
}

void VideoImage::clickedChooseFileBtn()
{
    QString dir = QFileDialog::getExistingDirectory(nullptr, "选择文件夹", m_Path, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    if(!dir.isEmpty()){
      m_Path = dir;
      m_VideoImagePath->setText(m_Path);
      XMLFunc::xmlValueModify(XMLFunc::ConfigXmlPath, XMLVideoImagePath, m_Path);
    }
}

void VideoImage::clickedVideoBtn()
{
    if(m_isRecord){
        senderValue(signalKind::VIDEO, btnStatus::Close);
    }else{
        senderValue(signalKind::VIDEO, btnStatus::Open);
    }
    m_isRecord = !m_isRecord;
}

void VideoImage::clickedImageBrn()
{
    senderValue(signalKind::IMAGE, btnStatus::Open);
}
